package vin.pth.base.pojo;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 默认的UserDetails实现.
 *
 * @author cocoon
 */
@Getter
@Setter
@NoArgsConstructor
public class DefaultUserDetails implements UserDetails {

  private Serializable userId;
  private String username;
  private String password;
  private Collection<GrantedAuthority> authority;
  private Collection<String> roles;


  /**
   * 默认的匿名用户.
   *
   * @return DefaultUserDetails
   */
  public static DefaultUserDetails anonymousUser() {
    DefaultUserDetails user = new DefaultUserDetails();
    user.setUserId("anonymous");
    user.setAuthority(new HashSet<>());
    user.setRoles(new HashSet<>(Collections.singleton("anonymous")));
    return user;
  }

  /**
   * 默认的超管用户.
   *
   * @return DefaultUserDetails
   */
  public static DefaultUserDetails adminUser(String password) {
    DefaultUserDetails user = new DefaultUserDetails();
    user.setUserId("admin");
    user.setUsername("admin");
    user.setPassword(password);
    user.setAuthority(new HashSet<>(Collections.singleton(new GrantedAuthority("*", "/*"))));
    user.setRoles(new HashSet<>(Collections.singleton("admin")));
    return user;
  }

}
